/******************************************************************************
 * FILE NAME:
 *  syslog_main.c
 *
 * DESCRIPTION:
 * This file contains all define for interface tp_ap which is interface
 * between Application-layer and Transport-layer
 *
 * DESIGN REFERENCE:
 *
 *
 * REVISION HISTORY   :
 * Date            Author               REASON
 * 09.15.2013      Thuan Nguyen         Creation
 *
 * Copyright 2005, NET
 * ***************************************************************************/
#include<signal.h>
#include <libxml/xmlreader.h>
#include"../h/syslog_main.h"

/***********************************************************************
 * FUNCTION NAME:
 *  oam_read_config_func
 *
 * DESCRIPTION:
 *	read config from file /configure/config.xml.
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
/*
U8 oam_read_config_func(U8 *filename)
{
    U8 test;
    U8 *xml_element;
    U8 *temp_level_log;
    U32 i;
    U32 selected_domain = 0;

    xmlTextReaderPtr reader;

    reader = xmlReaderForFile(filename, NULL, 0);

    if(SYSLOG_NULL == reader)
    {
    	SYSLOG_COMMON_ERROR_LOGGING("Cann't read file\n");
    }

    while(xmlTextReaderRead(reader))
    {
        switch(xmlTextReaderNodeType(reader))
        {
        	case XML_READER_TYPE_ELEMENT:
                xml_element = (U8 *)xmlTextReaderConstName(reader);
                xmlTextReaderMoveToElement(reader);

                if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_OAM_CONFIG))
                {
                	break;
                }else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_OAM_TRANSPORT))
                {
                	selected_domain = TRANSPORT_SYSLOG;
                	break;
                }else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_OAM_CONTENT))
                {
                	selected_domain = APPLICATION_SYSLOG;
                	break;
                }else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_OAM_APPLICATION))
                {
                	selected_domain = CONTENT_SYSLOG;
                	break;
                }else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_OAM_COMMON))
                {
                	selected_domain = COMMON_SYSLOG;
                	break;
                }else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_OAM_LOG_LEVEL))
                {
                	break;
                }
        	case XML_READER_TYPE_TEXT:
        		temp_level_log = (U8 *)xmlTextReaderConstValue(reader);

        		switch(selected_domain)
        		{
        		case TRANSPORT_SYSLOG:
        			temp_level_log
        			break;
        		case APPLICATION_SYSLOG:
        			break;
        		case CONTENT_SYSLOG:
        			break;
        		case COMMON_SYSLOG:
        			break;
        		default:
        			break;
        		}

        		if(SYSLOG_NULL == xml_element)
        		{
        			break;
        		}
        		if(SYSLOG_SUCCESS != strcmp(xml_element, XML_ELEMENT_OAM_TRANSPORT))
        		{
					if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANPORT_TEST_CLIENT))
					{
						g_config_info.client_list[client_number].client_id = client_number;
						printf("client_id: %d\n", g_config_info.client_list[client_number].client_id);
					}
					else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANPORT_TEST_PORT))
					{
						g_config_info.client_list[client_number].port = atoi(temp);
						printf("Port: %d\n", g_config_info.client_list[client_number].port);
					}
					else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANSPORT_TEST_IP_ADDRESS))
					{
						g_config_info.client_list[client_number].client_id = TESTM_ACTIVE_CLIENT_STATE;
						strcpy(g_config_info.client_list[client_number].ip_addr,temp);
						printf("ip_addr: %s\n", g_config_info.client_list[client_number].ip_addr);
					}
					else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANSPORT_TEST_CONTENT))
					{
						strcpy(g_config_info.client_list[client_number].content,temp);
						printf("Content: %s\n", g_config_info.client_list[client_number].content);
					}
					else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANSPORT_TEST_AMOUNT_PACKET))
					{
						g_config_info.client_list[client_number].amount = (U32)temp;
						printf("amount: %s\n", g_config_info.client_list[client_number].amount);
					}else{
						*xml_element = SYSLOG_NULL;
					}
        		}
        		break;

        	default:
        		break;
        }
    }

    xmlFreeTextReader(reader);
    xmlCleanupParser();

    return 0;
}
*/
/***********************************************************************
 * FUNCTION NAME:
 *  handle_level_string
 *
 * DESCRIPTION:
 *	create shared_memory for oam_mgmt and other processes.
 *
 * INPUT:
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void handle_level_string(U8* log_level,U32 *level)
{
	U32 temp_level;
	U32 index = 0;
	U32 length = 0;

	length = strlen(log_level);
	memset(level,0,sizeof(U32));
	for(index = 0;index < length;index++)
	{
		if(log_level[index] != ':')
		{
			temp_level = atoi(&log_level[index]);
			printf("temp_level %d\n",temp_level);
			switch(temp_level)
			{
			case INFO_SYSLOG:
				*level = (*level | (INFO_SYSLOG));
				break;
			case WARNING_SYSLOG:
				*level = *level | (WARNING_SYSLOG);
				break;
			case ERROR_SYSLOG:
				*level = *level | (INFO_SYSLOG << 2);
				break;
			case DEBUG_SYSLOG:
				*level = *level | (INFO_SYSLOG << 3);
				break;
			case DETAIL_SYSLOG:
				*level = *level | (INFO_SYSLOG << 4);
				break;
			default:
				break;
			}
		}
	}
}
/***********************************************************************
 * FUNCTION NAME:
 *  oam_create_shared_memory_func
 *
 * DESCRIPTION:
 *	create shared_memory for oam_mgmt and other processes.
 *
 * INPUT:
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *	trs_main()
 * NOTES:
 *
 ************************************************************************/
void oam_create_shared_memory_func()
{
}


/***********************************************************************
 * FUNCTION NAME:
 *  oam_create_trs_process_func
 *
 * DESCRIPTION:
 *	Inital process Transport
 *
 * INPUT:
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *	trs_main()
 * NOTES:
 *
 ************************************************************************/
U32 oam_create_trs_process_func()
{
	U32 val_result = SYSLOG_TRUE;
	pid_t pid;

	pid = fork();
	switch(pid)
	{
	case -1:
		SYSLOG_COMMON_ERROR_LOGGING("Create Application Process Failure.");
		return SYSLOG_FALSE;
	case 0:
		trs_main();
		break;
	default:
		break;
	}
	SYSLOG_COMMON_INFO_LOGGING("Create Transport Process successful.");
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME:
 *  oam_create_app_process_func
 *
 * DESCRIPTION:
 *	Inital process application
 *
 * INPUT:
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *	app_main()
 * NOTES:
 *
 ************************************************************************/
U32 oam_create_app_process_func()
{
	U32 val_result = SYSLOG_TRUE;
	pid_t pid;
	pid = fork();
	switch(pid){
	case -1:
		SYSLOG_COMMON_ERROR_LOGGING("Create Application Process Failure.");
		return SYSLOG_FALSE;
	case 0:
		app_main();
		break;
	default:
		break;
	}
	SYSLOG_COMMON_INFO_LOGGING("Create Application Process successful.");
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME:
 *  oam_create_content_process_func
 *
 * DESCRIPTION:
 *	Inital process content
 *
 * INPUT:
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *	init_master_thread_func()
 * NOTES:
 *
 ************************************************************************/
U32 oam_create_content_process_func()
{
	U32 val_result = SYSLOG_FALSE;
	pid_t pid;
	pid = fork();
	switch(pid){
	case -1:
		return val_result;
		break;
	case 0:
		val_result = SYSLOG_TRUE;
		break;
	default:
		break;

	}
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME:
 *  oam_mgmt_init
 *
 * DESCRIPTION:
 *	Inital process Transport,application and content
 *
 * INPUT:
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *  oam_create_trs_process_func()
 *  oam_create_app_process_func()
 *	oam_create_content_process_func()
 *	oam_mgmt_handle_func()
 * NOTES:
 *
 ************************************************************************/

void oam_mgmt_init()
{
	U32 val_result;
	if(SYSLOG_TRUE != oam_create_trs_process_func()){
		SYSLOG_COMMON_ERROR_LOGGING("TRS Process Creation is failure!");
	}	else
	if(SYSLOG_TRUE != oam_create_app_process_func()) {
		SYSLOG_COMMON_ERROR_LOGGING("APP Process Creation is failure!");
	} else/*
	if(SYSLOG_FALSE != oam_create_content_process_func()){
		SYSLOG_COMMON_ERROR_LOGGING("Content Process Creation is failure!");
	} else*/
	{
		oam_mgmt_handle_func();
	}
}
/***********************************************************************
 * FUNCTION NAME:
 *  main
 *
 * DESCRIPTION:
 *	Inital process Transport,application and content
 *
 * INPUT:
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *	init_master_thread_func()
 * NOTES:
 *
 ************************************************************************/
void oam_mgmt_handle_func()
{
	SYSLOG_COMMON_INFO_LOGGING("Enter function oam_mgmt_handle_func");
	U8 command[COMMAND_SIZE];
	while(1)
	{
		printf("Please enter your command:\n");
		printf("syslog programm >>>");

		while(!feof(stdin))
		{
			memset(command,0,sizeof(command));
			fgets(command,sizeof(command),stdin);
			printf("Command is: %s",command);

			printf("Please enter your command:\n");
			printf("syslog programm >>>");
		}
	}
}
/***********************************************************************
 * FUNCTION NAME:
 *  main
 *
 * DESCRIPTION:
 *	Inital process Transport,application and content
 *
 * INPUT:
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *	init_master_thread_func()
 * NOTES:
 *
 ************************************************************************/

U32 main(U32 argc,U8 **argv)
{
	oam_mgmt_init();
	return SYSLOG_TRUE;
}
